# PicOrdering.py
# v0.61 / 2008-02-16
# (c) Eol Ruin a.k.a. Arno Pucher 2008 - present

# v0.61 code improvement / cares for '*.jpg', '*.jpeg', '*.gif', '*.png', '*.avi', '*.mpg', '*.mpeg'
# v0.6 now cares on *.avi too
# v0.5 is still a little inefficient but works :)

version = '0.61'

print '--------------------------------------------------------'
print '# PicOrdering.py', ' / Version:', version
print '# (c) Eol Ruin a.k.a. Arno Pucher 2008 - present'
print '--------------------------------------------------------',

import os,glob,time,shutil

# init lists / define settings

filelist = []
allfilelist =[]

filetypes = ['*.jpg', '*.jpeg', '*.gif', '*.png', '*.avi', '*.mpg', '*.mpeg']

# put all needed files in allfilelist

for type in filetypes:
  for file in glob.glob(type):
    allfilelist.append(file)

 
# extract date & time
 
for file in allfilelist:
  stats = os.stat(file),
  # print len(stats),
  # print stats
  #print "-", stats[0][7] , "-"
  filedate = time.localtime(stats[0][8])
  year = filedate[0]
  month = filedate[1]
  day = filedate[2]

  #print filedate
  
  year_str = str(year)
  
  month_str = str(month)
  if len(month_str) == 1:
    month_str = '0' + month_str
  
  day_str = str(day)
  if len(day_str) == 1:
    day_str = '0' + day_str
	
  date_str = year_str + ' ' + month_str + ' ' + day_str
  
  filelist.append((file, date_str,stats[0][8]))
 
  
for entry in filelist:
  try:
    os.mkdir(entry[1])
  except:
    pass
  #print entry,
  src = entry[0]
  dst = entry[1]+'/'
  org_time = entry[2]
  access_times = org_time, org_time
  
  shutil.copy(src, dst)
  #print access_times,
  os.utime(dst+src, access_times) 
  print 'copied file: ', entry[0]
	
    
  